package com.ctgu.j08.controller;

import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.service.DUserService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.DUser;
import com.ctgu.j08.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/5/25 15:19
 */
@Controller
public class LoginController {

    @Resource
    DUserService dUserService;
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody @Valid DUser dUser)
    {
        DUser user = dUserService.getByUsername(dUser.getUserName());
        if(dUser==null || !user.getPassword().equals(dUser.getPassword()))
        {
            System.out.println("用户不存在|密码不正确");
            return ResultUtil.error(ResultEnum.LOGIN_FAILED);
        }
        System.out.println("登录成功");
        return ResultUtil.success("登录成功",user);
    }
//    public Result login(String username,String password)
//    {
//        System.out.println("用户名："+username+"密码："+password);
////        QueryWrapper<DUser> wrapper = new QueryWrapper<>();
////        wrapper.eq("user_name",username);
//        DUser dUser = dUserService.getByUsername(username);
//        System.out.println("查到的用户："+dUser);
//        System.out.println("密码："+dUser.getPassword());
////        DUser dUser=dUserService.getByUsername(username);
//        if(dUser==null || !dUser.getPassword().equals(password))
//        {
//            System.out.println("登录失败");
//            return ResultUtil.error(ResultEnum.LOGIN_FAILED);
//        }
//        System.out.println("登录成功");
//        return ResultUtil.success("登录成功",dUser);
//    }
        @GetMapping("/hello")
        public void test(){
            System.out.println("你好管理员");
        }
}
